package com.example.service.impl;

import com.example.domain.po.Role;
import com.example.domain.po.User;
import com.example.domain.po.UserList;
import com.example.domain.vo.PageResult;
import com.example.mapper.RoleMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserListService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserListServiceImpl implements UserListService {

    private final UserMapper userMapper;
    private final RoleMapper roleMapper;
    private final PasswordEncoder passwordEncoder;
    /**
     * 获取用户列表信息
     */
    @Override
    public PageResult getUserList(String  query,Integer currentPage,Integer pageSize) {

        //1.开启分页插件,设置分页参数,只对其后的一条sql有效
        PageHelper.startPage(currentPage,pageSize);
        // 2.执行查询
        List<User> resultUsers=userMapper.getUserList(query);
        //3.解析查询结果,并封装
        Page< User> page = (Page<User>) resultUsers;
        //4.获取总记录数
        Long total = page.getTotal();
        //5.获取当前页数据
        List<User> users = page.getResult();


        //添加用户角色信息(中文)
        List<UserList> usersList=new ArrayList<>();
        for (User user : users) {
            //
            UserList userList = new UserList(user);
            // 根据用户id获取用户所关联的角色id集合
            List<Integer> rolesId = roleMapper.batchGetRolesIdByUserId(user.getId());
            if(!CollectionUtils.isEmpty(rolesId)) {
                // 根据角色id集合获取角色信息
                List<String> roleNames = roleMapper.batchGetRolesByRoleId(rolesId).stream().map(Role::getRoleName).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(roleNames)) {
                   userList.setRoleNames(roleNames);
                }
            }
            usersList.add(userList);
        }
        log.info("用户列表信息：{}",usersList);
        // 返回用户列表信息
        return new PageResult(total, usersList);
    }

    /**
     * 修改用户状态
     */
    @Override
    public void changeUserStatus(Integer id, Boolean status, String roleNames) {
        // 获取用户角色信息
        String [] roleNameArray=roleNames.split(",");
        for(String roleName:roleNameArray){
            if("超级管理员".equals(roleName)){
                throw new RuntimeException("超级管理员无法禁用");
            }
        }
        userMapper.changeUserStatus(id,status);
    }
    /**
     * 删除用户
     */
    @Override
    public void deleteUser(Integer id, String roleNames) {
        // 获取用户角色信息
        String [] roleNameArray=roleNames.split(",");
        for(String roleName:roleNameArray){
            if("超级管理员".equals(roleName)){
                throw new RuntimeException("超级管理员不能删除");
            }
        }
        userMapper.deleteByPrimaryKey(id);
    }
    /**
     * 修改用户信息
     */
    @Override
    public void updateUser(Integer id, String email, String password) {
        if(!("".equals(password)||password==null)){
           password=passwordEncoder.encode(password);
        }
        userMapper.updateByPrimaryKeySelective(new User(id,email,password));
    }
}
